package com.spring.sunflower.base;

import android.app.Activity;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.enums.PopupAnimation;
import com.spring.sunflower.R;
import com.spring.sunflower.constant.Constant;
import com.spring.sunflower.dialog.HelpChoiceDialog;
import com.spring.sunflower.dialog.HelpDialog;
import com.spring.sunflower.dialog.NoticeDialog;
import com.spring.sunflower.dialog.OnDialogSelectListener;
import com.spring.sunflower.dialog.OnPermissionIntroClickListener;
import com.spring.sunflower.dialog.PermissionIntroDialog;
import com.spring.sunflower.util.DoubleClickUtils;
import com.spring.sunflower.util.LogUtils;
import com.umeng.analytics.MobclickAgent;

/**
 * Created by Kevin on 2021/5/19<br/>
 * <p>
 * Describe:<br/>
 */
public class AppBaseActivity extends AppCompatActivity {
    public String TAG = getClass().getSimpleName();
    public static boolean forbiddenBackKey = false;

    protected static BaseApplication mApp;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            if (forbiddenBackKey) {
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    public void printD(String msg) {
        LogUtils.INSTANCE.logD(TAG + "=>", msg);
    }

    public void printI(String msg) {
        LogUtils.INSTANCE.logI(TAG + "=>", msg);
    }

    public void printE(String msg) {
        LogUtils.INSTANCE.logE(TAG + "=>", msg);
    }

    public void printW(String msg) {
        LogUtils.INSTANCE.logW(TAG + "=>", msg);
    }

    public void showToast(String message) {
        ToastUtils.showShort(message);
    }

    public void showError(int resId) {
        showToast(getString(resId));
    }

    public void showLongToast(String message) {
        ToastUtils.showLong(message);
    }

    public void saveField(String key, String value) {
        SPUtils.getInstance().put(key, value);
    }

    public void saveField(String key, Boolean value) {
        SPUtils.getInstance().put(key, value);
    }

    public void saveField(String key, int value) {
        SPUtils.getInstance().put(key, value);
    }

    public String getField(String key, String defaultValue) {
        return SPUtils.getInstance().getString(key, defaultValue);
    }

    public boolean getField(String key, Boolean defaultValue) {
        return SPUtils.getInstance().getBoolean(key, defaultValue);
    }

    public int getField(String key, int defaultValue) {
        return SPUtils.getInstance().getInt(key, defaultValue);
    }

    public boolean isFastClick() {
        return DoubleClickUtils.isFastClick();
    }

    public boolean isFastShortClick() {
        return DoubleClickUtils.isFastShortClick();
    }

    public int getColorX(int colorId) {
        return ContextCompat.getColor(this, colorId);
    }

    public String format(String x) {
        if (TextUtils.isEmpty(x)) {
            return "";
        }
        return String.valueOf(Double.valueOf(x).intValue());
    }

    public void clearStorageUserData() {
        saveField(Constant.KEY_TOKEN, "");
        saveField(Constant.KEY_GENDER, "");
        saveField("birthday", "");
        saveField(Constant.KEY_SIGNATURE, "");
        saveField(Constant.KEY_NICKNAME, "");
        saveField(Constant.KEY_TYPE, "");
        saveField(Constant.KEY_USERID, "");
        saveField(Constant.KEY_PHOTO, "");
        saveField(Constant.KEY_MOBILE, "");
        saveField(Constant.KEY_WEALTH, "");
        saveField(Constant.KEY_USERID_CHATTER, "");
        saveField(Constant.KEY_AVATAR_CHATTER, "");
        saveField(Constant.KEY_NICKNAME_CHATTER, "");
        saveField(Constant.KEY_CHATTER_PRICE, "");
        saveField(Constant.CHATTED_TIME_STORAGE, "");
        saveField(Constant.KEY_APPLY_WITHDRAW, "");
    }

    protected void clearCurrentActivity() {
        if (mApp != null) {
            Activity currentActivity = mApp.getCurrentActivity();
            if (this.equals(currentActivity)) {
                mApp.setCurrentActivity(null);
            }
        }
    }

    public void showPermissionIntroDialog(BasePopupView popupView) {
        new XPopup.Builder(this)
                .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
                .isRequestFocus(false)
                .dismissOnTouchOutside(false)
                .dismissOnBackPressed(false)
                .asCustom(popupView)
                .show();
    }

    public void showPermissionIntroDialog(String title, String desc, @Nullable OnPermissionIntroClickListener listener) {
        PermissionIntroDialog dialog = new PermissionIntroDialog(this, R.layout.dialog_permission_intro, listener);
        dialog.setOnCreateViewListener(new PermissionIntroDialog.OnCreateViewListener() {
            @Override
            public void onCreateView() {
                TextView tvTitle = dialog.findViewById(R.id.tvTitle);
                TextView tvDesc = dialog.findViewById(R.id.tvDesc);
                tvTitle.setText(title);
                tvDesc.setText(desc);
            }
        });
        new XPopup.Builder(this)
                .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
                .isRequestFocus(false)
                .dismissOnTouchOutside(false)
                .dismissOnBackPressed(false)
                .asCustom(dialog)
                .show();
    }

    public void showPermissionIntroDialog(String[] titleArr, String[] descArr, OnPermissionIntroClickListener listener) {
        PermissionIntroDialog dialog = new PermissionIntroDialog(this, R.layout.dialog_permission_intro_list, listener);
        dialog.setOnCreateViewListener(new PermissionIntroDialog.OnCreateViewListener() {
            @Override
            public void onCreateView() {
                LinearLayout container = dialog.getContainer();
                for (int i = 0; i < titleArr.length; i++) {
                    View inflate = View.inflate(getBaseContext(), R.layout.dialog_permission_intro_content_item, null);
                    TextView tvTitle = inflate.findViewById(R.id.tvTitle);
                    TextView tvDesc = inflate.findViewById(R.id.tvDesc);
                    tvTitle.setText(titleArr[i]);
                    tvDesc.setText(descArr[i]);
                    container.addView(inflate);
                }
            }
        });


        new XPopup.Builder(this)
                .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
                .isRequestFocus(false)
                .dismissOnTouchOutside(false)
                .dismissOnBackPressed(false)
                .asCustom(dialog)
                .show();
    }

    public void showNoticeDialog(@Nullable String headTitle, String title, String desc, @Nullable OnPermissionIntroClickListener listener) {
        NoticeDialog dialog = new NoticeDialog(this, listener);
        dialog.setOnCreateViewListener(new NoticeDialog.OnCreateViewListener() {
            @Override
            public void onCreateView() {
                TextView tv = dialog.findViewById(R.id.tv);
                TextView tvTitle = dialog.findViewById(R.id.tvTitle);
                TextView tvDesc = dialog.findViewById(R.id.tvDesc);
                tvTitle.setText(title);
                tvDesc.setText(desc);
                if (!TextUtils.isEmpty(headTitle)) {
                    tv.setText(headTitle);
                }
            }
        });
        new XPopup.Builder(this)
                .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
                .isRequestFocus(false)
                .dismissOnTouchOutside(false)
                .dismissOnBackPressed(false)
                .asCustom(dialog)
                .show();
    }

    /**
     * 通知弹窗
     * 大标题可以根据返回的dialog来设置
     *
     * @param title
     * @param desc     内容描述
     * @param listener
     * @return
     */
    public NoticeDialog showNoticeDialog(String title, String desc, @Nullable OnPermissionIntroClickListener listener) {
        NoticeDialog dialog = new NoticeDialog(this, listener);
        dialog.setOnCreateViewListener(new NoticeDialog.OnCreateViewListener() {
            @Override
            public void onCreateView() {
                TextView tv = dialog.findViewById(R.id.tv);
                TextView tvTitle = dialog.findViewById(R.id.tvTitle);
                TextView tvDesc = dialog.findViewById(R.id.tvDesc);
                tvTitle.setText(title);
                tvDesc.setText(desc);
            }
        });
        new XPopup.Builder(this)
                .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
                .isRequestFocus(false)
                .dismissOnTouchOutside(false)
                .dismissOnBackPressed(false)
                .asCustom(dialog)
                .show();
        return dialog;
    }

    public NoticeDialog showNoticeDialog(String tvText, String title, String desc, String okText, @Nullable OnPermissionIntroClickListener listener) {
        NoticeDialog dialog = new NoticeDialog(this, listener);
        dialog.setOnCreateViewListener(new NoticeDialog.OnCreateViewListener() {
            @Override
            public void onCreateView() {
                TextView tv = dialog.findViewById(R.id.tv);
                TextView tvTitle = dialog.findViewById(R.id.tvTitle);
                TextView tvDesc = dialog.findViewById(R.id.tvDesc);
                tvTitle.setText(title);
                tvDesc.setText(desc);
                tv.setText(tvText);
                TextView tvOK = dialog.findViewById(R.id.tvOK);
                tvOK.setText(okText);
            }
        });
        new XPopup.Builder(this)
                .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
                .isRequestFocus(false)
                .dismissOnTouchOutside(false)
                .dismissOnBackPressed(false)
                .asCustom(dialog)
                .show();
        return dialog;
    }
    public NoticeDialog showNoticeDialog(String tvText, String title, String desc, String okText, String cancelText, @Nullable OnPermissionIntroClickListener listener) {
        NoticeDialog dialog = new NoticeDialog(this, listener);
        dialog.setOnCreateViewListener(new NoticeDialog.OnCreateViewListener() {
            @Override
            public void onCreateView() {
                TextView tv = dialog.findViewById(R.id.tv);
                TextView tvTitle = dialog.findViewById(R.id.tvTitle);
                TextView tvDesc = dialog.findViewById(R.id.tvDesc);
                tvTitle.setText(title);
                tvDesc.setText(desc);
                tv.setText(tvText);
                TextView tvOK = dialog.findViewById(R.id.tvOK);
                tvOK.setText(okText);
                TextView tvCancel = dialog.findViewById(R.id.tvCancel);
                tvCancel.setText(cancelText);
            }
        });
        new XPopup.Builder(this)
                .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
                .isRequestFocus(false)
                .dismissOnTouchOutside(false)
                .dismissOnBackPressed(false)
                .asCustom(dialog)
                .show();
        return dialog;
    }
    public HelpDialog showHelpDialog(String title, String desc, @Nullable OnPermissionIntroClickListener listener) {
        HelpDialog dialog = new HelpDialog(this, listener);
        dialog.setOnCreateViewListener(new HelpDialog.OnCreateViewListener() {
            @Override
            public void onCreateView() {
                TextView tv = dialog.findViewById(R.id.tv);
                tv.setText(title);
                TextView tvDesc = dialog.findViewById(R.id.tvDesc);
                tvDesc.setText(desc);
            }
        });
        new XPopup.Builder(this)
                .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
                .isRequestFocus(false)
                .dismissOnTouchOutside(false)
                .asCustom(dialog)
                .show();
        return dialog;
    }
    public HelpChoiceDialog showHelpChoiceDialog(@NonNull OnDialogSelectListener listener){
        HelpChoiceDialog dialog = new HelpChoiceDialog(this, listener);
        new XPopup.Builder(this)
                .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
                .isRequestFocus(false)
                .dismissOnTouchOutside(false)
                .asCustom(dialog)
                .show();
        return dialog;
    }
}
